﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<%--
Copyright (C) 2004-2006  Autodesk, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of version 2.1 of the GNU Lesser
General Public License as published by the Free Software Foundation.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
--%>
<%@ Page Language="C#" Debug="true"  ValidateRequest="false"%>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web.SessionState" %>
<%@ Import Namespace="OSGeo.MapGuide" %>

<!-- #Include File="utilityfunctions.aspx -->

<html>
  <head>
    <title>Viewer Sample Application - List Selected Parcels</title>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta http-equiv="content-script-type" content="text/javascript" />
    <meta http-equiv="content-style-type" content="text/css" />
    <link href="../../styles/globalstyles.css" rel="stylesheet" type="text/css">
    <link href="../../styles/alphastyles.css" rel="stylesheet" type="text/css">
    <link href="../../viewerfiles/viewer.css" rel="stylesheet" type="text/css">
  </head>
  <body class="AppFrame" dir=rtl bgcolor="">
    <h1 class="AppHeading">חישוב שטח/אורך</h1>
    <hr width=100%/>
    
    <%
        String mgSessionId = GetParameters()["SESSION"];
        String mgSelection = HttpUtility.UrlDecode(Request["SEL"]);
        String mgUNITS = Request["UNITS"];
        String mgENUNITS = Request["ENTYPE"];
        String mgMapName = Request["MAPNAME"];
        MgResourceIdentifier layerFeatureResource;
        MgReadOnlyLayerCollection layers;
        MgSelection selection;
        MgLayer layer;
        string layerClassName;
        string selectionString;
        string layerFeatureId;
        double mgArea = 0;
        double mgLength = 0;
        string measurmsg = "";


        //Response.Write("session: " + mgSessionId + "<BR><BR>Sel: " + HttpUtility.UrlDecode(mgSelection) + "<BR><BR>UNITS: " + mgUNITS + "<BR>ENUNITS: " + mgENUNITS + "<BR>MapName: " + mgMapName);
        //Response.End();
        try
        {
            InitializeWebTier();

            MgUserInformation userInfo = new MgUserInformation(mgSessionId);
            MgSiteConnection siteConnection = new MgSiteConnection();
            siteConnection.Open(userInfo);

            MgResourceService resourceService = siteConnection.CreateService(MgServiceType.ResourceService) as MgResourceService;
            MgFeatureService featureService = siteConnection.CreateService(MgServiceType.FeatureService) as MgFeatureService;
            MgFeatureQueryOptions queryOptions = new MgFeatureQueryOptions();

            MgMap map = new MgMap();
            map.Open(resourceService, mgMapName);

            selection = new MgSelection(map, mgSelection);
            layers = selection.GetLayers();

            if (layers != null)
            {
                Response.Write("<P class='SubTitle'>תוצאת חישוב לבחירה במפה</p>");
                for (int i = 0; i < layers.GetCount(); i++)
                {
                    layer = (MgLayer)layers.GetItem(i);
                    // Create a filter containing the IDs of the selected features on this layer
                    layerClassName = layer.GetFeatureClassName();
                    selectionString = selection.GenerateFilter(layer, layerClassName);

                    
                    // Get the feature resource for the selected layer
                    layerFeatureId = layer.GetFeatureSourceId();
                    layerFeatureResource = new MgResourceIdentifier(layerFeatureId);

                    // Apply the filter to the feature resource for the selected layer. This returns
                    // an MgFeatureReader of all the selected features.
                    queryOptions.SetFilter(selectionString);
                    MgFeatureReader featureReader = featureService.SelectFeatures(layerFeatureResource, layerClassName, queryOptions);
                    //Response.Write(selectionString.ToString());
                    //Response.End();
                    
                    // Process each item in the MgFeatureReader, displaying the owner name
                    int j = 0;
                    while (featureReader.ReadNext())
                    {
                        MgByteReader featureAgfGeometry;
                        try
                        {
                            featureAgfGeometry = featureReader.GetGeometry("Geometry");
                        }
                        catch (Exception)
                        {
                            featureAgfGeometry = featureReader.GetGeometry("Geom");
                        }
                        
                        MgAgfReaderWriter agfReaderWriter = new MgAgfReaderWriter();
                        MgGeometry featureGeometry = agfReaderWriter.Read(featureAgfGeometry);
                        mgLength += featureGeometry.GetLength();
                        mgArea += featureGeometry.GetArea();
                        if (featureGeometry.IsClosed())
                            mgLength = 0;
                        else
                            mgArea = 0;
                        j+=1;
                    }
                    Response.Write("<p>");
                    if(mgArea != 0)
                        measurmsg = layer.GetLegendLabel() + " (" + j + "): <font style='background-color:LightGrey; padding: 2px'>" + (mgArea < 1000 ? mgArea.ToString("00.00") : mgArea.ToString("0,000.00")) + "</font>  מ''ר<BR>";
                    else
                        measurmsg = layer.GetLegendLabel() + " (" + j + "): <font style='background-color:LightGrey; padding: 2px'>" + (mgLength < 1000 ? mgLength.ToString("00.00") : mgLength.ToString("0,000.00")) + "</font>  מ'<BR>";
                    Response.Write(measurmsg+"</p><br>");
                    mgArea = 0; mgLength = 0;
                 }
            }
            else
                Response.Write("No selected layers</p>");

        }
        catch (MgException mge)
        //catch (InvalidCastException mge)
        {
            Response.Write(mge.Message);
        }
        finally
        {
            Response.Write("<P class='InfoText'>באפשרותך לבחור מספר פרטים מכמה שכבות בו זמנית, ולהציג את תוצאת חישוב השטח מקובצת לשכבות.<BR>לדוגמא אם בחרת 3 מבנים שכ''א מהם 10 ממ'ר, אזי יוצג 30 ממ'ר כתוצאה.</p>");
        }
        


    %>
    <p style='background-color:LightGrey'></p>
  </body>

</html>
